﻿using System;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfShopIndexColSet : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                loadGoodslist();
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }
        private void loadGoodslist()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCommercialPageColumn odataobj = new AppLogic.DataAlc.CBizShpCommercialPageColumn())
            {
                DataSet ds = odataobj.DataObj.QueryBySQL(@"
	SELECT 
	C.PAGE_COL_ID
	,C.PAGE_COL_NME
	,C.PAGE_COL_TITLE
	,C.COL_SEQ_NUM
	,C.IS_COL_SHOW
	,C.ROW_COLSPAN_NUM
	,C.TEMPLATE_STYLE_CID 
	,I.PAGE_COL_ITEM_ID
	,I.SEQ_NUM
	,I.GOODS_ID
	,A.GOODS_NME
	,A.IMG_URL
	FROM  
	SHP_COMMERCIAL_PAGE_COLUMN C
	LEFT OUTER JOIN SHP_COMMERCIAL_PAGE_COLUMN_ITEM I ON C.PAGE_COL_ID=I.PAGE_COL_ID
	LEFT OUTER JOIN SHP_GOODS_INFO A   ON I.GOODS_ID=A.GOODS_ID
	WHERE
	C.PAGE_TYPE_NME='店铺首页' AND (C.COMM_TENANT_ID='" + msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0] + @"') ORDER BY C.COL_SEQ_NUM ASC,I.SEQ_NUM ASC
");

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], ""
                    , "PAGE_COL_ID,PAGE_COL_NME,PAGE_COL_TITLE,COL_SEQ_NUM,IS_COL_SHOW,ROW_COLSPAN_NUM,TEMPLATE_STYLE_CID"
                    , 1, 1000, "COL_SEQ_NUM ASC,SEQ_NUM ASC");
                //                this.ExecJScript(@"debugger;
                //                "); 
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                doBoundGoodsList( data,'');");
            }
        }

        private void dobtnSave()
        {
            string strcolcount = this.GetParamHtValue("PAGE_COL_COUNT");
            int icolcount = 0;
            if (int.TryParse(strcolcount, out icolcount))
            {
                icolcount = int.Parse(strcolcount);
            }
            if (icolcount <= 0)
            {
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizShpCommercialPageColumn objcolc = new AppLogic.DataAlc.CBizShpCommercialPageColumn())
            {
                DataSet dscol=objcolc.SelectByShopId(msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0]);
                System.Collections.Generic.List<string> lscolid = new System.Collections.Generic.List<string>();
                for (int i = 0; i < icolcount; i++)
                {
                    DataRow drcol;
                    DataRow[] arrrowtm1 = dscol.Tables[0].Select("PAGE_COL_ID='" + GetParamHtValue("PAGE_COL_ID_"+i.ToString()) + "'");
                    if (arrrowtm1.Length > 0)
                    {
                        drcol = arrrowtm1[0];
                    }
                    else
                    {
                        drcol = dscol.Tables[0].NewRow();
                        drcol["PAGE_COL_ID"] = objcolc.DataObj.NewID();
                        drcol["COMM_TENANT_ID"] = msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0];
                        drcol["PAGE_TYPE_NME"] = "店铺首页";
                        dscol.Tables[0].Rows.Add(drcol);
                    }
                    drcol["COL_SEQ_NUM"] = GetParamHtValue("COL_SEQ_NUM_" + i.ToString());
                    drcol["PAGE_COL_NME"] = GetParamHtValue("PAGE_COL_NME_" + i.ToString());
                    drcol["PAGE_COL_TITLE"] = GetParamHtValue("PAGE_COL_TITLE_" + i.ToString());
                    drcol["TEMPLATE_STYLE_CID"] = GetParamHtValue("TEMPLATE_STYLE_CID_" + i.ToString());
                    drcol["ROW_COLSPAN_NUM"] = GetParamHtValue("ROW_COLSPAN_NUM_" + i.ToString());
                    drcol["IS_COL_SHOW"] = GetParamHtValue("IS_COL_SHOW_" + i.ToString())=="True";
                    lscolid.Add(drcol["PAGE_COL_ID"].ToString());


                    using (msBsc.AppLogic.DataAlc.CBizShpCommercialPageColumnItem objcolitem = new AppLogic.DataAlc.CBizShpCommercialPageColumnItem())
                    {
                        System.Collections.Generic.List<string> lscolitemid = new System.Collections.Generic.List<string>();
                        DataSet dscolitem = objcolitem.SelectByColId(drcol["PAGE_COL_ID"].ToString());
                        int igoodsCont = int.Parse(GetParamHtValue(i.ToString() + "_COUNT"));
                        for (int r = 0; r < igoodsCont; r++)
                        {
                            DataRow drcolitem;
                            DataRow[] arrrowtem2 = dscolitem.Tables[0].Select("GOODS_ID='" + this.GetParamHtValue("GOODS_ID_" + i.ToString() + "_" + r.ToString()) + "'");
                            if (arrrowtem2.Length > 0)
                            {
                                drcolitem = arrrowtem2[0];
                            }
                            else
                            {
                                drcolitem = dscolitem.Tables[0].NewRow();
                                drcolitem["PAGE_COL_ITEM_ID"] = objcolitem.DataObj.NewID();
                                drcolitem["GOODS_ID"] = this.GetParamHtValue("GOODS_ID_" + i.ToString() + "_" + r.ToString());
                                drcolitem["PAGE_COL_ID"] = drcol["PAGE_COL_ID"];
                                drcolitem["COMM_TENANT_ID"] = msBsc.SvRef.UserInfo.OperDealerId.Split(',')[0];
                                dscolitem.Tables[0].Rows.Add(drcolitem);
                            }
                            drcolitem["SEQ_NUM"] = r;
                            lscolitemid.Add(drcolitem["GOODS_ID"].ToString());
                        }

                        DataRow[] arrtemtt2 = dscolitem.Tables[0].Select("GOODS_ID NOT IN ('"+string.Join("','",lscolitemid)+"')");
                        for (int k = arrtemtt2.Length - 1; k >= 0; k--)
                        {
                            arrtemtt2[k].Delete();
                        }
                        objcolitem.Update(dscolitem);
                    }
                }

                DataRow[] arrtemtt1 = dscol.Tables[0].Select("PAGE_COL_ID NOT IN ('" + string.Join("','", lscolid) + "')");
                for (int k = arrtemtt1.Length - 1; k >= 0; k--)
                {
                    arrtemtt1[k].Delete();
                }
                objcolc.Update(dscol);
            }

        }


        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (strAction == "loadGoodslist")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                loadGoodslist();
            }
            else if (strAction == "btnSave")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    dobtnSave();
                    ts.Complete();
                }
                this.ExecJScript("layer.msg('已成功保存！',1,{type:1,shade:false});");
                loadGoodslist();
            }

            base.InitialByParam();
        } 

    }
}